/**********************************************************************************
***
***    Screencasting Teaching System C++ Library
***    Copyright (C) 2011  Jiri Novak <jiri.novak@petriny.net>
***                        Wadi Jalil Maluf <wadijm@gmail.com>
***
***    This file is part of the Screencasting Teaching System C++ Library.
***
***    This library is free software; you can redistribute it and/or modify
***    it under the terms of the GNU General Public License as published by
***   the Free Software Foundation; either version 3 of the License, or
***    (at your option) any later version.
***
***    This library is distributed in the hope that it will be useful,
***    but WITHOUT ANY WARRANTY; without even the implied warranty of
***    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
***   GNU General Public License for more details.
***
***    You should have received a copy of the GNU General Public License
***    along with this library; if not, write to the Free Software
***    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
***
*********************************************************************************/

#ifndef SCREENCASTING_TEACHING_SYSTEM___CLIENT__SERVER_SERVICE_H_
#define SCREENCASTING_TEACHING_SYSTEM___CLIENT__SERVER_SERVICE_H_

#include <list>
#include <log4cxx/logger.h>

#include "../common/comm/sockettcp.h"
#include "../common/comm/thread.h"
#include "../common/comm/protocol.h"
#include "../common/comm/operation.h"
#include "../common/comm/mutex.h"
#include "../common/model/user.h"
#include "../common/model/lesson.h"
#include "servercallbackhandler.h"

/*
 * Clase encargada de comunicarse con el servidor. Es la encargada de abrir el socket tcp para la comunicación al login. Una vez establecida la comunicación, envía los comandos y los procesa llamando a los métodos del handler configurado.
 *
 *  Created on: 03/06/2011
 *      Author: wadi
 */

class ServerService: public Thread {
public:
	static ServerService& getInstance(); // singleton
	virtual ~ServerService();
	bool initComunication(std::string url, int port);
	void stop();
	void login(std::string username, std::string password);
	void logout();
	void findAllShows();
	void sentChatMessage(ChatMessage* msg);
	void sendScreencastData(Lesson *lesson, unsigned char *data, int size,int pts);
	void sendScreencastAudioData(Lesson *lesson, unsigned char *data, int size,int pts);
	void startScreencast(Lesson *lesson);
	void startAndSaveScreencast(Lesson *lesson);
	void playScreencast(Lesson *lesson);
	void stopScreencast(Lesson *lesson);
	void setHandler(ServerCallbackHandler *handler) { this->handler = handler; }
	void answerRecieved(Operation& op);
	void run();
	void stopListeningScreencast(Lesson * lesson);

private:
	log4cxx::LoggerPtr logger_;

	ServerService();                      // singleton
	ServerService(ServerService const&);  // singleton
	void operator=(ServerService const&); // singleton

	SocketTcp socket;
	Protocol protocol;
	ServerCallbackHandler *handler;
	bool opened;
	void recieveMessage(Operation& op);
	Mutex mutex;
};

#endif /* SCREENCASTING_TEACHING_SYSTEM___CLIENT__SERVER_SERVICE_H_ */
